package com.kiln.juxingqs.ui.vm

import android.app.Application
import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import client.global.commlib.network.data.JsonResult
import client.global.commlib.network.data.Ret
import client.global.commlib.toast.toast
import client.global.commlib.utils.StoreUtil
import com.kiln.juxingqs.api.bean.GetDddBatchRet
import com.kiln.juxingqs.api.bean.QueryUser
import com.kiln.juxingqs.api.bean.UserAddress
import com.kiln.juxingqs.api.bean.UserInfo
import com.kiln.juxingqs.ui.viewmodel.MiddleVm

class ApplyDeliveryViewModel(val app: Application) : MiddleVm(app) {
    val call = MutableLiveData<JsonResult<Ret>>()
    val queryRet = MutableLiveData<JsonResult<QueryUser>>()
    val isShowing = MutableLiveData<Boolean>()

    val goods_id = MutableLiveData<String>("")
    val sNum = MutableLiveData<String>("")
    val pick_type = MutableLiveData<String>("")

    val shipAccount = MutableLiveData<String>("")
    val shipAddress = MutableLiveData<String>("")
    val detailAddress = MutableLiveData<String>("")

    //    val contactSite = MutableLiveData<String>("")
    val contactPhone = MutableLiveData<String>("")
//    val deliveryBatch = MutableLiveData<GetDddBatchRet.Batch>()

    val sh_province = MutableLiveData<String>("")
    val sh_city = MutableLiveData<String>("")
    val sh_district = MutableLiveData<String>("")
    val sh_place = MutableLiveData<String>("")
    val userRet = MutableLiveData<JsonResult<UserInfo>>()

    val addR = MutableLiveData<UserAddress.Address>()


    val userInfo = MutableLiveData<UserInfo>().apply {
        value = StoreUtil.getBean<UserInfo>(app)
    }

    fun userInfo() {
        hold({
            server.userinfo().apply {
                body()?.let {
                    if (it.isSuccess) {
                        StoreUtil.saveBean(getApplication(), it.data!!)
                    }
                }
            }
        }, success = {
            userRet.value = it
        })
    }

    /**
     */
    fun ok() {
        if (TextUtils.isEmpty(sNum.value)
        ) {
            toast("请输入提货数量")
            return
        }

        if (addR.value == null) {
            toast("请选择地址")
            return
        }

//        if (TextUtils.isEmpty(shipAccount.value)
//        ) {
//            toast("请输入收货人姓名")
//            return
//        }
//        if (TextUtils.isEmpty(sh_province.value)
//            || TextUtils.isEmpty(sh_city.value)
//            || TextUtils.isEmpty(sh_district.value)
//        ) {
//            toast("请选择收货地址")
//            return
//        }
////        println("$sh_province $sh_city $sh_district")
//        if (TextUtils.isEmpty(detailAddress.value)
//        ) {
//            toast("请输入详细地址")
//            return
//        }
//        if (TextUtils.isEmpty(contactPhone.value)
//        ) {
//            toast("请输入手机号")
//            return
//        }


//
//        @Field("num") num: String,
//        @Field("sh_name") sh_name: String,
//        @Field("sh_phone") sh_phone: String,
//
//        @Field("sh_province") sh_province: String,
//        @Field("sh_city") sh_city: String,
//        @Field("sh_district") sh_district: String,
//        @Field("sh_place") sh_place: String,

        hold({
            server.pick_up_v2(
                goods_id.value!!,
                sNum.value!!,
                pick_type.value!!,
                addR.value!!.address_id
//                shipAccount.value!!,
//                contactPhone.value!!,
//                sh_province.value!!,
//                sh_city.value!!,
//                sh_district.value!!,
//                sh_place.value!!,
            )
        }, {
            isShowing.value = true
        }, {
            call.value = it
        }, {

        }, {
            isShowing.value = false
        })


    }


}